﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;

namespace TS_XY_Train_Repaire.UserCon.Locomotive.Technology
{
    public partial class uc_Locomotive_MainBase_WorkFlow_TrainWorkGroup : DevExpress.XtraEditors.XtraForm
    {
        public string _LoadID, _LoadMWID, _LoadMWDID, _LoadMWSID, _LoadType, _LoadName, _LoadOrder, _LoadLength, _LoadStime, _LoadEtime = "";

        private void cb_Type_CheckedChanged(object sender, EventArgs e)
        {
            if (cb_Type.Checked)
            {
                cb_Type.Text = "卡控作业点";
            }
            else
            {
                cb_Type.Text = "非卡控作业点";
            }
            _SetButtonEnabled();
        }

        private void bt_Exit_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
        }

        private void bt_Sure_Click(object sender, EventArgs e)
        {
            string strSQL = "";
            string f_type = "0";
            string f_timetype = "0";
            if (rb_0.Checked)
            {
                f_timetype = "0";
            }
            if (rb_1.Checked)
            {
                f_timetype = "1";
            }
            if (rb_2.Checked)
            {
                f_timetype = "2";
            }
            if (rb_3.Checked)
            {
                f_timetype = "3";
            }
            if (cb_Type.Checked)
            {
                f_type = "1";
            }
            if (_LoadType == "Add")
            {
                strSQL = "insert into TS_TR_PS_Work_Main_Group (f_mwid,f_mwdid,f_mwsid,f_name,f_length,f_order,f_workerid,f_stime,f_etime,f_type,f_timetype,f_pnum) values(" + _LoadMWID + "," + _LoadMWDID + "," + _LoadMWSID + ",'" + tb_Name.Text.Trim() + "'," + tb_Length.Text.Trim() + "," + tb_Order.Text.Trim() + "," + TS_Win_Locomotive_MainForm.pUserInfoMain.GuId + ",'" + tb_Stime.Text.Trim() + "','" + tb_Etime.Text.Trim() + "',"+f_type+","+f_timetype+","+tb_PNum.Text.Trim()+")";
            }
            if (_LoadType == "Edit")
            {
                strSQL = "update TS_TR_PS_Work_Main_Group set f_name='" + tb_Name.Text.Trim() + "',f_length=" + tb_Length.Text.Trim() + ",f_order=" + tb_Order.Text.Trim() + ",f_workerid=" + TS_Win_Locomotive_MainForm.pUserInfoMain.GuId + ",f_time=getdate(),f_stime='" + tb_Stime.Text.Trim() + "',f_etime='" + tb_Stime.Text.Trim() + "',f_type=" + f_type + ",f_timetype=" + f_timetype + ",f_pnum=" + tb_PNum.Text.Trim() + " where f_id=" + _LoadID;
            }
            if (_LoadType == "Del")
            {
                strSQL = "update TS_TR_PS_Work_Main_Group set f_del=1,f_workerid=" + TS_Win_Locomotive_MainForm.pUserInfoMain.GuId + ",f_time=getdate() where f_id=" + _LoadID;
            }
            TS_Win_Class.MySqlCom(strSQL);
            this.DialogResult = DialogResult.OK;
        }

        private void uc_Locomotive_MainBase_WorkFlow_TrainWorkGroup_Load(object sender, EventArgs e)
        {
            if (_LoadType == "Add")
            {
                bt_Sure.Text = "确定新增";
            }
            else
            {
                if (_LoadType == "Edit")
                {
                    bt_Sure.Text = "确定修改";
                }
                else
                {
                    bt_Sure.Text = "确定删除";
                }
                string[] _str = TS_Win_Class.GetSQlFieldValue("select * from TS_TR_PS_Work_Main_Group where f_id=" + _LoadID, new string[] { "f_stime", "f_etime", "f_length", "f_name", "f_order", "f_type", "f_timetype", "f_pnum" });
                tb_Stime.Text = _str[0];
                tb_Etime.Text = _str[1];
                tb_Length.Text = _str[2];
                tb_Name.Text = _str[3];
                tb_Order.Text = _str[4];
                if (_str[5] == "0")
                {
                    cb_Type.Checked = false;
                }
                else
                {
                    cb_Type.Checked = true;
                }
                switch(_str[6])
                {
                    case "0":
                        rb_0.Checked = true;
                        break;
                    case "1":
                        rb_1.Checked = true;
                        break;
                    case "2":
                        rb_2.Checked = true;
                        break;
                    case "3":
                        rb_3.Checked = true;
                        break;
                }
                tb_PNum.Text = _str[7];
            }
        }
        private void _SetButtonEnabled()
        {
            if (tb_Order.Text.Trim().Length > 0 && tb_Name.Text.Trim().Length > 0 && tb_Length.Text.Trim().Length > 0 && tb_Stime.Text.Trim().Length == 5 && tb_Etime.Text.Trim().Length == 5 && tb_PNum.Text.Trim().Length>0)
            {
                bt_Sure.Enabled = true;
            }
            else
            {
                bt_Sure.Enabled = false;
            }
        }

        private void tb_Name_TextChanged(object sender, EventArgs e)
        {
            _SetButtonEnabled();
        }

        public uc_Locomotive_MainBase_WorkFlow_TrainWorkGroup()
        {
            InitializeComponent();
        }
    }
}